package thinkingInJava.arrays;

//: arrays/CopyingArrays.java
//Using System.arraycopy()
import java.util.*;
import static thinkingInJava.YTool.YPrint.*;

class Point
{
 int x;
int y;
Point(int x,int y)
{
this.x = x;
this.y = y;
}
}

public class CopyingArrays {
public static void main(String[] args) {
 int[] i = new int[7];
 int[] j = new int[10];
 Arrays.fill(i, 47);
 Arrays.fill(j, 99);
 println("i = " + Arrays.toString(i));
 println("j = " + Arrays.toString(j));
 System.arraycopy(i, 0, j, 0, i.length);
 println("j = " + Arrays.toString(j));
 int[] k = new int[5];
 Arrays.fill(k, 103);
 System.arraycopy(i, 0, k, 0, k.length);
 println("k = " + Arrays.toString(k));
 Arrays.fill(k, 103);
 System.arraycopy(k, 0, i, 0, k.length);
 println("i = " + Arrays.toString(i));
 // Objects:
 Integer[] u = new Integer[10];
 Integer[] v = new Integer[5];
 Arrays.fill(u, new Integer(47));
 Arrays.fill(v, new Integer(99));
 println("u = " + Arrays.toString(u));
 println("v = " + Arrays.toString(v));
 System.arraycopy(v, 0, u, u.length/2, v.length);
 println("u = " + Arrays.toString(u));
 
 Point [] src = new Point[] {new Point(1,1),new Point(2,2)};
 Point [] dest = new Point[2];
 System.arraycopy(src,0,dest,0,src.length);
 dest[0].x = 111;	// 修改 dest src 也变了
 println(src[0].x);
 println(dest==src);
 println(dest[0]==src[0]);
}
} /* Output:
i = [47, 47, 47, 47, 47, 47, 47]
j = [99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
j = [47, 47, 47, 47, 47, 47, 47, 99, 99, 99]
k = [47, 47, 47, 47, 47]
i = [103, 103, 103, 103, 103, 47, 47]
u = [47, 47, 47, 47, 47, 47, 47, 47, 47, 47]
v = [99, 99, 99, 99, 99]
u = [47, 47, 47, 47, 47, 99, 99, 99, 99, 99]
*///:~
